package com.siwei.mes.controller.tc;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.sync.tc.SupplyTask;
import com.siwei.mes.entity.sync.tc.Waybill;
import com.siwei.mes.entity.sync.tc.WaybillEntity;
import com.siwei.mes.entity.vo.WaybillVo;
import com.siwei.mes.mapper.syncTc.SupplyTaskMapper;
import com.siwei.mes.service.sync.tc.WaybillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * TC-运单控制层
 *
 * @author yangke
 * @since 2024-05-27 15:45:00
 */
@Api(value = "TC-运单")
@RestController
@RequestMapping("tcWaybill")
public class WaybillController {
    @Resource
    private WaybillService waybillService;
    @Resource
    private SupplyTaskMapper supplyTaskMapper;

    @ApiOperation(value = "修改运单")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) Waybill entity) {
        waybillService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取TC运单")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Waybill, Waybill> query) {
        return Json.succ().data(waybillService.getPage(query));
    }

    @ApiOperation(value = "绑定运单列表")
    @PostMapping("/bindPage")
    public Json bindPage(@RequestBody @Validated(PageGroup.class) PageQuery<Waybill, Waybill> query) {
        return Json.succ().data(waybillService.bindPage(query));
    }

    @ApiOperation(value = "根据主键id获取TC运单")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(waybillService.loadById(id));
    }

    @ApiOperation(value = "获取所有TC运单")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Waybill entity) {
        return Json.succ().data(ApiResult.addList(waybillService.getAll(entity)));
    }

    @ApiOperation(value = "根据试验台账ID获取所有运单")
    @GetMapping("selectByExperimentId")
    public Json selectByExperimentId(@RequestParam("experimentId") Long experimentId, @RequestParam(required = false, name = "supplyTaskId") Long supplyTaskId) {
        Long newTaskId = null;
        /**这里传的id是 supply_task表里面的主键id*/
        if (supplyTaskId != null) {
            SupplyTask supplyTask = supplyTaskMapper.loadById(supplyTaskId);
            if (supplyTask != null) {
                newTaskId = supplyTask.getSupplyTaskId();
            }
        }
        return Json.succ().data(waybillService.selectByExperimentId(experimentId, newTaskId));
    }

    @ApiOperation(value = "根据试验台账ID获取所有运单")
    @PostMapping("selectByExperimentIdPage")
    public Json selectByExperimentIdPage(@RequestBody @Validated(PageGroup.class) PageQuery<WaybillVo, WaybillVo> query) {
        return Json.succ().data(waybillService.selectByExperimentIdPage(query));
    }

    @ApiOperation(value = "批量根据运单生成试验委托")
    @PostMapping("batchGenerateExperiment")
    public Json batchGenerateExperiment(@RequestBody WaybillEntity waybillEntity) {
        waybillService.batchGenerateExperiment(waybillEntity);
        return Json.succ().data(new ApiResult());
    }
}
